package com.crl.ssc.dic.dao;

import com.cars.util.page.PageUtil;
import com.cars.util.string.StringUtil;
import com.crl.dao.BaseDao;
import com.crl.ssc.dic.model.DicInfo;
import com.crl.ssc.dic.vo.DicInfoSearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @description: 基础信息-企业企业Dao
 * @author: MqSql
 * @version: 1.0
 * @date: 2020-08-06 18:16:54
 */
@Repository
public class DicInfoDao extends BaseDao<DicInfo, DicInfoSearchVO> {
    /**
     * 新增
     *
     * @param dicInfo
     * @return
     */
    public int add(DicInfo dicInfo) {
        String sql = "INSERT INTO NCP_ENT_INFO(ent_code,ent_name) " +
                "VALUES(:entCode,:entName)";
        return insert(sql, dicInfo);
    }

    /**
     * 修改
     *
     * @param dicInfo
     * @return
     */
    public int update(DicInfo dicInfo) {
        String sql = "UPDATE NCP_ENT_INFO SET ent_code=:entCode,ent_name=:entName " +
                    "WHERE id=:id";
        return update(sql, dicInfo);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Integer id) {
        String sql = "DELETE FROM NCP_ENT_INFO WHERE id=?";
        return delete(sql, id);
    }
    /**
     * 列表
     *
     * @param dicInfoSearchVO
     * @return
     */
    public List<DicInfo> list(DicInfoSearchVO dicInfoSearchVO) {
        String sql = "SELECT id,ent_code,ent_name FROM NCP_ENT_INFO WHERE 1=1 ";
        sql += createSearchSQL(dicInfoSearchVO);
        sql += " ORDER BY id ASC";
        sql = PageUtil.createMysqlPageSql(sql, dicInfoSearchVO.getPage());
        return list(sql, dicInfoSearchVO);
    }
    /**
     * 查询数据条数
     *
     * @param dicInfoSearchVO
     * @return
     */
    public int count(DicInfoSearchVO dicInfoSearchVO) {
        String sql = "SELECT COUNT(id) FROM NCP_ENT_INFO WHERE 1=1 ";
        sql += createSearchSQL(dicInfoSearchVO);
        return count(sql, dicInfoSearchVO);
    }
    /**
       * 拼接查询条件
       *
       * @param dicInfoSearchVO
       * @return
    */
    private String createSearchSQL(DicInfoSearchVO dicInfoSearchVO) {
        String sql = "";
        if (StringUtil.isNotNullOrEmpty(dicInfoSearchVO.getEntCode())) {
			sql += " AND ent_code =:entCode ";
		}
        if (StringUtil.isNotNullOrEmpty(dicInfoSearchVO.getEntName())) {
			sql += " AND ent_name =:entName ";
		}
        return sql;
    }

    /**
     * 获取单个
     *
     * @param id
     * @return
     */
    public DicInfo get(Integer id) {
        String sql = "SELECT id,ent_code,ent_name FROM NCP_ENT_INFO WHERE id=?";
        return get(sql, id);
    }
}
